package com.yuyou.train.user.controller;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yuyou.train.service.api.user.dto.UserFeedbackPageDTO;
import com.yuyou.train.service.api.user.entity.UserFeedback;
import com.yuyou.train.user.service.IUserFeedbackService;
import com.yuyou.train.user.vo.req.ReqUserFeedback;
import com.yuyou.train.util.bean.LoginRedisObj;
import com.yuyou.train.util.bean.PageParams;
import com.yuyou.train.util.bean.Pagination;
import com.yuyou.train.util.bean.ReturnResult;
import com.yuyou.train.util.enums.user.UserFeedbackEnums;
import com.yuyou.train.util.tool.DateUtil;
import com.yuyou.train.util.tool.DefindBeanUtils;
import com.yuyou.train.util.tool.Sequence;
import com.yuyou.train.util.tool.UUIDUtil;

/**
 * Created by yujin.liu on 2018/1/30.
 */
@Validated
@RestController
@RequestMapping(value="/api/userFeedback")
public class UserFeedbackController extends BaseController{

    @Autowired
    private IUserFeedbackService userFeedbackService;


    @ApiOperation(value="用户反馈分页列表")
    @ApiImplicitParam(name = "status", value = "反馈信息处理状态，0 未处理，1 已处理", paramType="query")
    @RequestMapping(value="/findUserFeedbackPage",method = {RequestMethod.POST, RequestMethod.GET})
    public ReturnResult<Pagination<UserFeedbackPageDTO>> finduserCommentPage(@Validated @ModelAttribute PageParams pageParams,
                                                                             @RequestParam(value = "status", required = false) String status,
                                                                             @RequestParam(value = "startTime", required = false) String startTime,
                                                                             @RequestParam(value = "endTime", required = false) String endTime){

        Map<String, Object> conditions = new HashMap<>();
        conditions.put("status", status);
        conditions.put("startTime", startTime);
        conditions.put("endTime", endTime);
        Pagination<UserFeedbackPageDTO> queryPgn = new Pagination<>();
        queryPgn.setConditions(conditions);
        queryPgn.setPageParams(pageParams);
        int total =  userFeedbackService.findUserFeedbackPageCount(queryPgn);
        queryPgn.setTotal(total);
        queryPgn.toBePage();
        if(total > 0){
            queryPgn.setTotal(total);
            List<UserFeedbackPageDTO> list =  userFeedbackService.findUserFeedbackPageList(queryPgn);
            queryPgn.setList(list);
        }
        return ReturnResult.success(queryPgn);
    }


    @ApiOperation(value="反馈处理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "反馈 id", paramType="query", required = true),
            @ApiImplicitParam(name = "status", value = "反馈信息处理状态，0 未处理，1 已处理", paramType="query", required = true)
    })
    @RequestMapping(value="/changeFeedbackStatus",method = {RequestMethod.POST, RequestMethod.GET})
    public ReturnResult<UserFeedback> changeFeedbackStatus(@NotBlank(message="id不能为空") String id,
                                                           @NotBlank(message="status不能为空") String status){

        UserFeedback userFeedback = new UserFeedback();
        userFeedback.setId(id);
        userFeedback.setStatus(status);
        userFeedbackService.updateByPrimaryKeySelective(userFeedback);

        return ReturnResult.success(userFeedback);
    }


    @ApiOperation(value="删除反馈")
    @ApiImplicitParam(name = "id", value = "反馈 id", paramType="query", required = true)
    @RequestMapping(value="/delUserFeedback",method = {RequestMethod.POST, RequestMethod.GET})
    public ReturnResult<Void> delUserFeedback(@NotBlank(message="id不能为空") String id){
        userFeedbackService.deleteByPrimaryKey(id);
        return ReturnResult.success("OK");
    }


    @ApiOperation(value="新增反馈(前台)")
    @RequestMapping(value="/addUserFeedback",method = {RequestMethod.POST, RequestMethod.GET})
    public ReturnResult<Void> addUserFeedback(@Validated @ModelAttribute ReqUserFeedback reqUserFeedback,
                                              HttpServletRequest request){
        LoginRedisObj loginObj = this.getUserObj(request);

        UserFeedback userFeedback = new UserFeedback();
        DefindBeanUtils.copyProperties(reqUserFeedback, userFeedback);
        userFeedback.setId(Sequence.getSequence().getNextId());
        userFeedback.setUserId(loginObj.getUserId());
        userFeedback.setStatus(UserFeedbackEnums.status.unhandle.getValue());
        userFeedback.setCreateTime(DateUtil.now());
        userFeedbackService.insert(userFeedback);
        return ReturnResult.success("OK");
    }

    @ApiOperation(value="我的反馈(前台)")
    @RequestMapping(value="/myFeedbackPage",method = RequestMethod.POST)
    public ReturnResult<Pagination<UserFeedbackPageDTO>> myFeedbackPage(@Validated @ModelAttribute PageParams pageParams,
                                                                        HttpServletRequest request){

        LoginRedisObj loginObj = this.getUserObj(request);
        Map<String, Object> conditions=new HashMap<>();
        Pagination<UserFeedbackPageDTO> queryPgn = new Pagination<>();
        conditions.put("userId", loginObj.getUserId());

        queryPgn.setConditions(conditions);
        queryPgn.setPageParams(pageParams);
        int total = userFeedbackService.findUserFeedbackPageCount(queryPgn);
        queryPgn.setTotal(total);
        queryPgn.toBePage();
        if(total > 0){
            queryPgn.setTotal(total);
            List<UserFeedbackPageDTO> list = userFeedbackService.findUserFeedbackPageList(queryPgn);
            queryPgn.setList(list);
        }
        return ReturnResult.success(queryPgn);
    }

}
